package org.sacinfo.controller;

import static org.apache.shiro.web.filter.authc.FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
	@Controller
public class LoginController {
	@RequestMapping("login")
	public String login(HttpServletRequest request,ModelMap model) throws Exception{
		//如果登陆失败从request中获取认证异常信息，shiroLoginFailure就是shiro异常类的全限定名
		//String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
		String exceptionClassName =(String)request.getAttribute(DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		if(exceptionClassName!=null){
			if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
				model.addAttribute("error", "账号不存在");
			} else if (IncorrectCredentialsException.class.getName().equals(
					exceptionClassName)) {
				model.addAttribute("error", "用户名/密码错误");
			} else if("randomCodeError".equals(exceptionClassName)){
				model.addAttribute("error", "验证码错误");
			}else {
				model.addAttribute("error", "请输入正确的账号和密码");
			}
		}
		//此方法不处理登陆成功（认证成功），shiro认证成功会自动跳转到上一个请求路径
		//登陆失败还到login页面
		return "login";
	}
}
